#encoding: utf-8

class CyMapsController < ApplicationController
  before_filter :clean_page, :only => [:index]

  def index
    begin
    format = request.headers['HTTP_ACCEPT']
    unless format.include?('application/json')
      @cy = current_user.biz_unit
      init_conditions
    end
    shipowner_id = params[:shipowner_id]
    forward_id = params[:forward_id]
    port_id = params[:port_id]
    voyage = params[:voyage]
    route = params[:route]
    course = params[:course]
    bl_no = params[:bl_no]
    is_on_way = params[:is_on_way]

    conditions = {}
    if shipowner_id and shipowner_id.strip != ""
      conditions[:shipowner_id] = shipowner_id.strip
    end
    if forward_id and forward_id.strip != ""
      conditions[:forward_id] = forward_id.strip
    end
    if port_id and port_id.strip != ""
      conditions[:port_id] = port_id.strip
    end
    if voyage and voyage.strip != ""
      conditions[:voyage] = voyage.strip
    end
    if route and route.strip != ""
      conditions[:route] = route.strip
    end
    if course and course.strip != ""
      conditions[:course] = course.strip
    end
    if bl_no and bl_no.strip != ""
      conditions[:bl_no] = bl_no.strip
    end

    conditions = init_identity_condition(conditions)
    if is_on_way == 'true'
      @bls = Bl.query_ctn_on_way_bls(conditions, params[:page]).all
    else
      @bls = Bl.query_bls(conditions, params[:page])
    end

    flash.now[:notice] = "查询完成"
    rescue Exception => ex
      logger.error "#{ex.class}, #{ex.message}"
      flash.now[:notice] = "系统异常，稍候再试"
    end
    respond_to do |format|
      format.html { render :layout => "common" } # index.html.erb
      format.json { render :json => {:notice => flash.now[:notice], :html => render_to_string(:template => '/utils/_bl_and_ctns.html.erb')} }
    end
  end

  private
  def init_conditions
    @routes ||= @cy.routes
    @courses ||= @cy.courses
    @forwards ||= @cy.forwards
    @ports ||= @cy.ports
    @voyages ||= @cy.voyages
    @shipowners ||= @cy.shipowners

  end

end
